@model ProductModel

@await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.ProductDetailsProductAttributesCombinationsTop, additionalData = Model })
<div class="panel panel-default">
    <div class="panel-body">
        <p>
            @T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Description")
        </p>

        <div id="attributecombinations-grid"></div>

        <script>
            $(document).ready(function() {
                $("#attributecombinations-grid").kendoGrid({
                    dataSource: {
                        type: "json",
                        transport: {
                            read: {
                                url: "@Html.Raw(Url.Action("ProductAttributeCombinationList", "Product"))",
                                type: "POST",
                                dataType: "json",
                                data: function additionalData() {
                                    var data = {
                                        ProductId: '@(Model.Id)'
                                    };
                                    addAntiForgeryToken(data);
                                    return data;
                                }
                            },
                            destroy: {
                                url: "@Html.Raw(Url.Action("ProductAttributeCombinationDelete", "Product"))",
                                type: "POST",
                                dataType: "json",
                                data: addAntiForgeryToken
                            }
                        },
                        schema: {
                            data: "Data",
                            total: "Total",
                            errors: "Errors",
                            model: {
                                id: "Id",
                                fields: {
                                    AttributesXml: { editable: false, type: "string" },
                                    Warnings: { editable: false, type: "string" },
                                    StockQuantity: { editable: true, type: "number" },
                                    AllowOutOfStockOrders: { editable: true, type: "boolean" },
                                    Sku: { editable: true, type: "string" },
                                    ManufacturerPartNumber: { editable: true, type: "string" },
                                    Gtin: { editable: true, type: "string" },
                                    OverriddenPrice: { editable: true, type: "number" },
                                    NotifyAdminForQuantityBelow: { editable: true, type: "number" },
                                    Id: { editable: false, type: "number" }
                                }
                            }
                        },
                        error: function(e) {
                            display_kendoui_grid_error(e);
                            // Cancel the changes
                            this.cancelChanges();
                        },
                        pageSize: @(Model.ProductAttributeCombinationSearchModel.PageSize),
                        serverPaging: true,
                        serverFiltering: true,
                        serverSorting: true
                    },
                    pageable: {
                        refresh: true,
                        pageSizes: [@(Model.ProductAttributeCombinationSearchModel.AvailablePageSizes)],
                        @await Html.PartialAsync("_GridPagerMessages")
                    },
                    editable: {
                        confirmation: "@T("Admin.Common.DeleteConfirmation")",
                        mode: "inline"
                    },
                    scrollable: false,
                    columns: [
                        {
                            field: "AttributesXml",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.Attributes")",
                            width: 400,
                            template: "#=AttributesXml# # if(Warnings !== null) {# <div><span style=\"color: red\">#=Warnings#</span></div> #}  #",
                            encoded: false
                        }, {
                            field: "StockQuantity",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.StockQuantity")",
                            width: 150,
                            //integer format
                            format: "{0:0}"
                        }, {
                            field: "AllowOutOfStockOrders",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.AllowOutOfStockOrders")",
                            width: 150,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template: '# if(AllowOutOfStockOrders) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                        }, {
                            field: "Sku",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.Sku")",
                            width: 200
                        }, {
                            field: "ManufacturerPartNumber",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.ManufacturerPartNumber")",
                            width: 200
                        }, {
                            field: "Gtin",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.Gtin")",
                            width: 200
                        }, {
                            field: "OverriddenPrice",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.OverriddenPrice")",
                            width: 200,
                            editor: function(container, options) {
                                $('<input name="' + options.field + '"/>')
                                    .appendTo(container)
                                    .kendoNumericTextBox({
                                        format: "{0:n4}",
                                        decimals: 4
                                    });
                            }
                        }, {
                            field: "NotifyAdminForQuantityBelow",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.NotifyAdminForQuantityBelow")",
                            width: 150,
                            //integer format
                            format: "{0:0}"
                        }, {
                            field: "PictureThumbnailUrl",
                            title: "@T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.Fields.Picture")",
                            template: '<img src="#=PictureThumbnailUrl#" />',
                            width: 200
                        }, {
                            field: "Id",
                            title: "@T("Admin.Common.Edit")",
                            width: 100,
                            headerAttributes: { style: "text-align:center" },
                            attributes: { style: "text-align:center" },
                            template:
                                "<button onclick=\"javascript:OpenWindow('@Url.Content("~/Admin/Product/ProductAttributeCombinationEditPopup/")#=Id#?btnId=btnRefreshCombinations&formId=product-form', 800, 700, true); return false;\" class='btn btn-default'><i class=\"fa fa-pencil\"></i>@T("Admin.Common.Edit")</button>"
                        }, {
                            command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                            title: "@T("Admin.Common.Delete")",
                            width: 150
                        }
                    ]
                });
            });
        </script>
    </div>
    <div class="panel-footer">
        <button type="submit" id="btnAddNewCombination" onclick="javascript:OpenWindow('@(Url.Action("ProductAttributeCombinationCreatePopup", "Product", new { productId = Model.Id, btnId = "btnRefreshCombinations", formId = "product-form" }))', 800, 700, true); return false;" class="btn btn-primary">
            @T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.AddNew")
        </button>
        <button type="button" id="btnGenerateAllCombinations" class="btn bg-aqua">
            @T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.GenerateAll")
        </button>
        <nop-action-confirmation asp-button-id="btnGenerateAllCombinations" />
        <button type="submit" id="btnGenerateSeveralCombination" onclick="javascript:OpenWindow('@(Url.Action("ProductAttributeCombinationGeneratePopup", "Product", new { productId = Model.Id, btnId = "btnRefreshCombinations", formId = "product-form" }))', 800, 700, true); return false;" class="btn bg-aqua">
            @T("Admin.Catalog.Products.ProductAttributes.AttributeCombinations.GenerateSeveral")
        </button>
        <input type="submit" id="btnRefreshCombinations" style="display: none" />
        <script type="text/javascript">
            $(document).ready(function() {
                $('#btnRefreshCombinations').click(function() {
                    //refresh grid
                    var grid = $("#attributecombinations-grid").data('kendoGrid');
                    grid.dataSource.read();

                    //return false to don't reload a page
                    return false;
                });

                //generate combinations
                $("#btnGenerateAllCombinations-action-confirmation-submit-button").bind("click", function () {
                    var postData = {

                    };
                    addAntiForgeryToken(postData);
                    $.ajax({
                        cache: false,
                        type: "POST",
                        url: '@Url.Action("GenerateAllAttributeCombinations", "Product", new { productId = Model.Id })',
                        data: postData,
                        dataType: 'json',
                        complete: function (data) {
                            //reload grid
                            var grid = $('#attributecombinations-grid').data('kendoGrid');
                            grid.dataSource.read();
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert('Error while generating attribute combinations');
                        }
                    });
                    $('#btnGenerateAllCombinations-action-confirmation').modal('toggle');
                    return false;
                });
            });
        </script>
    </div>
</div>
@await Component.InvokeAsync("AdminWidget", new { widgetZone = AdminWidgetZones.ProductDetailsProductAttributesCombinationsBottom, additionalData = Model })
